package com.practice.gmall.realtime.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: CommonUtil
 * Package: com.practice.gmall.realtime.util
 * Description: 公共工具类
 *
 * @Author lzy.ethan@qq.com
 * @Create 2023-02-17 10:38
 */
public class CommonUtil {
    /**
     * @param str
     * @Description: 将输入的字符串切分成尽量细小的粒度
     * @return: List<Lexeme> 将输出的字符返回
     * @Author: lzy
     * @Date:2023-02-17 - 10:48
     */
    public static List<String> splitZnWords(String str) throws IOException {
        List<String> words = new ArrayList<>();

        StringReader reader = new StringReader(str);
        IKSegmenter ikSegmenter = new IKSegmenter(reader, true);

        Lexeme next = ikSegmenter.next();
        while (next != null) {
            String word = next.getLexemeText();
            words.add(word);

            next = ikSegmenter.next();
        }
        return words;
    }

    /**
     * @param str
     * @Description: 将小驼峰变量名转化为下划线变量名
     * @return: java.lang.String
     * @Author: lzy
     * @Date:2023-02-18 - 16:41
     */
    public static String smallCamelCaseToUnderscore(String str) {
        String regex = "([a-z])([A-Z]+)";
        String replacement = "$1_$2";
        return str.replaceAll(regex, replacement).toLowerCase();
    }
}
